package edu.xidian.bdilab.jrbigdata.service.admin.governmentadmin.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.admin.AdminCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;

import edu.xidian.bdilab.jrbigdata.domain.BankUser;
import edu.xidian.bdilab.jrbigdata.domain.GovernmentUser;
import edu.xidian.bdilab.jrbigdata.mapper.GovermentUserMapper;
import edu.xidian.bdilab.jrbigdata.service.admin.governmentadmin.governmentadminService;
import edu.xidian.bdilab.jrbigdata.util.DecryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import edu.xidian.bdilab.jrbigdata.mapper.GovernmentInfoMapper;
import  edu.xidian.bdilab.jrbigdata.domain.GovernmentInfo;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.List;
@Service
public class governmentadminServiceimpl implements governmentadminService{

    @Autowired
   GovernmentInfoMapper governmentInfoMapper;
    @Autowired
    GovermentUserMapper govermentUserMapper;

    @Override
    public ResponseResult governmentManage_check(GovernmentInfo governmentInfo) throws Exception
    {
        ResponseResult responseResult=new ResponseResult();
        QueryWrapper<GovernmentInfo> queryWrapper=new QueryWrapper<>();
        if(governmentInfo.getFkUserName()!=null&&governmentInfo.getFkUserName().length()!=0){
            queryWrapper.like("fk_user_name",governmentInfo.getFkUserName());
        }
        if(governmentInfo.getDepartmentName()!=null&&governmentInfo.getDepartmentName().length()!=0){
            queryWrapper.eq("department_name",governmentInfo.getDepartmentName());
        }
        if(governmentInfo.getPrincipalName()!=null&&governmentInfo.getPrincipalName().length()!=0){
            queryWrapper.eq("principal_name",governmentInfo.getPrincipalName());
        }
        if(governmentInfo.getPrincipalPhone()!=null&&governmentInfo.getPrincipalPhone().length()!=0){
            queryWrapper.eq("principal_phone",governmentInfo.getPrincipalPhone());
        }
        if(governmentInfo.getGovernmentCode()!=0){
            queryWrapper.eq("government_code",governmentInfo.getGovernmentCode());
        }
        try{
            List<GovernmentInfo> governmentInfoList=governmentInfoMapper.selectList(queryWrapper);
            if(governmentInfoList.size()!=0)
            {
                responseResult.setData(governmentInfoList);
                responseResult.setSuccess(Boolean.TRUE);
                responseResult.setCode(AdminCode.SelectSUCCESS.getCode());
                responseResult.setMessage(AdminCode.SelectSUCCESS.getMessage());
            }
            else
            {
                responseResult.setSuccess(Boolean.TRUE);
                responseResult.setCode(AdminCode.NOT_EXIST.getCode());
                responseResult.setMessage(AdminCode.NOT_EXIST.getMessage());
            }
            return responseResult;
        }catch (Exception e){
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.SelectFailed.getCode());
            responseResult.setMessage(AdminCode.SelectFailed.getMessage());
            return responseResult;
        }
    }

    @Override
    public  ResponseResult governmentManage_add(GovernmentInfo governmentInfo,String password) throws Exception{
        ResponseResult responseResult=new ResponseResult();
        GovernmentUser governmentUser=new GovernmentUser();
        governmentUser.setUserName(governmentInfo.getFkUserName());
        String md5Password = DecryptUtils.getMd5(password);
        governmentUser.setPassword(md5Password);
        QueryWrapper<GovernmentUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name", governmentInfo.getFkUserName());
        GovernmentUser g = govermentUserMapper.selectOne(userQueryWrapper);
        if(g==null){
            govermentUserMapper.insert(governmentUser);
        }else{
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.NOT_EXIST.getCode());
            responseResult.setMessage("新增政府联系人失败");
        }
        int result=governmentInfoMapper.insert(governmentInfo);
        if(result==1)
        {
            responseResult.setSuccess(Boolean.TRUE);
            responseResult.setCode(AdminCode.SelectSUCCESS.getCode());
            responseResult.setMessage("新增政府联系人成功");
        }
        else {
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.NOT_EXIST.getCode());
            responseResult.setMessage("新增政府联系人失败");
        }
        return responseResult;
    }

    @Override
    public  ResponseResult governmentManage_update(GovernmentInfo governmentInfo,String password) throws Exception{
        ResponseResult responseResult=new ResponseResult();
        GovernmentUser governmentUser=new GovernmentUser();
        governmentUser.setUserName(governmentInfo.getFkUserName());
        String md5Password = DecryptUtils.getMd5(password);
        governmentUser.setPassword(md5Password);
        QueryWrapper<GovernmentUser> g=new QueryWrapper<>();
        g.eq("user_name",governmentUser.getUserName());
        int r=govermentUserMapper.update(governmentUser,g);
        int result=governmentInfoMapper.updateById(governmentInfo);
        if(result==1&&r==1)
        {
            responseResult.setSuccess(Boolean.TRUE);
            responseResult.setCode(AdminCode.SelectSUCCESS.getCode());
            responseResult.setMessage("编辑政府联系人成功");
        }
        else {
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.NOT_EXIST.getCode());
            responseResult.setMessage("编辑政府联系人失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult  governmentManage_delete(String userName)throws Exception
    {

        ResponseResult responseResult=new ResponseResult();
        try{
            QueryWrapper<GovernmentInfo> governmentInfoQueryWrapper=new QueryWrapper<>();
            governmentInfoQueryWrapper.eq("fk_user_name",userName);
            governmentInfoMapper.delete(governmentInfoQueryWrapper);
            QueryWrapper<GovernmentUser> governmentUserQueryWrapper=new QueryWrapper<>();
            governmentUserQueryWrapper.eq("user_name",userName);
            govermentUserMapper.delete(governmentUserQueryWrapper);
            responseResult.setSuccess(Boolean.TRUE);
            responseResult.setCode(AdminCode.DELEATESUCCESS.getCode());
            responseResult.setMessage(AdminCode.DELEATESUCCESS.getMessage());
            return responseResult;
        }catch (Exception e){
            responseResult.setSuccess(Boolean.FALSE);
            responseResult.setCode(AdminCode.DELEATEFAILED.getCode());
            responseResult.setMessage(AdminCode.DELEATEFAILED.getMessage());
            return responseResult;
        }
    }
}
